-- select_target
-- create by panyl
-- 需要客户端选定目标的陷阱

return {
    getTargets = function(trapId, pos, targetPos)
        local info = CommonTrapM.query(trapId);
        local trapType = info["type"];
        local feature = info["feature"];

        assert( trapType == TRAP_TYPE_TARGET and type(targetPos) == "number" or
                feature == TRAP_FEATURE_MIN_HP or
                feature == TRAP_FEATURE_MAX_HP, "不是选择目标型的陷阱！");

        if type == TRAP_TYPE_TARGET then
            return { DungeonM.getGridByPos(targetPos), };
        end

        local target;
        local refHp = MAX_VALUE;
        if feature == TRAP_FEATURE_MAX_HP then
            refHp = -1;
        end

        for pos = 1, GRID_SIZE do
            repeat
                local grid = DungeonM.getGridByPos(pos);
                local monster = grid.monster;

                -- 格子未开、不是怪物、或者已经死亡都忽略
                if  not grid:isOpened() or
                    not grid:isMonster() or
                    not monster or
                    monster:isDead() then
                    break;
                end

                if  feature == TRAP_FEATURE_MIN_HP and
                    -- 选择最小血量
                    refHp > monster:getHp() then
                    target = grid;
                    refHp = monster:getHp();
                elseif feature == TRAP_FEATURE_MAX_HP and
                    -- 选择最大血量
                    refHp < monster:getHp() then
                    target = grid;
                    refHp = monster:getHp();
                end
            until true;
        end

        if target then
            return { target, };
        end

        return {};
    end,
};
